package collection;

import groupby.manage.GroupbyManage;
import groupby.model.Groupby;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import dic.manage.DicManage;
import dic.model.City;



public class KuTuan {

	
	public static void startCollection(ApplicationContext context)
	{

		String urlStr="http://www.kutuan.com/tuangou/api";
		StringBuffer s = new StringBuffer();
		try {
			URL url = new URL(urlStr);
			try {
				SAXReader reader = new SAXReader();
				Document doc = reader.read(url);
				Groupby groupby=new Groupby();
				
				GroupbyManage groupbyManage=(GroupbyManage)context.getBean("groupbyManage");
				List list = doc.selectNodes("//tuangous/tuangou");
				
				for(Iterator it=list.iterator();it.hasNext();)
				{
					groupbyManage=(GroupbyManage)context.getBean("groupbyManage");
					Element element=(Element)it.next();
					Node title = element.selectSingleNode("title");
					
					Node dealUrl = element.selectSingleNode("url");
					Node image =element.selectSingleNode("image");
					Node cityId =element.selectSingleNode("city");
					DicManage dicManage=(DicManage)context.getBean("dicManage");
					City city=dicManage.findCityByName(cityId.getText());
					if(city==null||city.getFlag()!=1)
					{
						break;
					}
					boolean isFind=groupbyManage.isFindGroupby(title.getText(),city.getId());
					if(!isFind)
					{
						continue;
					}
					Node startDate=element.selectSingleNode("beginTime");
					Node endDate=element.selectSingleNode("endTime");
					Node price=element.selectSingleNode("price");
					Node originalCost=element.selectSingleNode("primaryPrice");
					Node discountPercent=element.selectSingleNode("ratio");
					
					
					groupby.setTitle(title.getText());
					groupby.setDealUrl(dealUrl.getText());
					groupby.setImage(image.getText());
					
					
					groupby.setCityId(city.getId());
					
					SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					Date date=new Date();
					
					date.setTime(Long.parseLong(startDate.getText()+"000"));
					groupby.setStartDate(sdf.format(date));
					date.setTime(Long.parseLong(endDate.getText()+"000"));
					groupby.setEndDate(sdf.format(date));
					
					groupby.setPrice(Float.parseFloat(price.getText()));
					groupby.setOriginalCost(Float.parseFloat(originalCost.getText()));
					groupby.setDiscountPercent(discountPercent.getText());
					groupby.setDiscountAmount(Float.parseFloat(originalCost.getText())-Float.parseFloat(price.getText()));
					groupby.setTopCount(0);
					groupby.setInputDatetime(sdf.format(new Date()));
					groupby.setWebsite("酷团网");
					
					
					
					groupbyManage.save(groupby);
				}
				
				
			} catch (Exception e) {

				e.printStackTrace();

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
